import {Base64} from 'js-base64';
import CryptoJS from 'crypto-js';


const PUBLIC_KEY = import.meta.env.VITE_APP_SIGN_KEY

//参数校验
function validateParams(timestamp, randomId, appKey) {
  // 你可以根据实际情况调整这些验证逻辑
  if(isNaN(timestamp)) {
    console.error("Invalid timestamp");
    return false;
  }
  if(randomId.length === 0) {
    console.error("Invalid randomId");
    return false;
  }
  if(appKey.length === 0) {
    console.error("Invalid appKey");
    return false;
  }
  return true;
}

export class AppSign {
  //签名
  static getAppSign (timestamp, randomId, appKey) {
    // 输入验证
    if(!validateParams(timestamp, randomId, appKey)) {
      return "";
    }
    try {
      return Base64.encode(CryptoJS.MD5(PUBLIC_KEY + appKey + timestamp + randomId).toString());
    } catch (error) {
      // 异常处理：记录错误或返回一个错误标识
      console.error("Error generating app sign:", error);
      return "";
    }
  }
}

